//
//  GXScanViewController.m
//  ScanQRCode
//
//  Created by Ssky on 2016/11/21.
//  Copyright © 2016年 Sky SS. All rights reserved.
//

#import "GXScanViewController.h"
#import "GXQRPreview.h"

@interface GXScanViewController () <GXQRCodeManagerDelegate>

@end

@implementation GXScanViewController

- (instancetype)init {
    if (self = [super init]) {
        _manager = [GXQRCodeManager manager];
        self.manager.codeDelegate = self;
        [self setupUI];
    }
    return self;
}

- (void)loadView {
    GXQRPreview * preview = [[GXQRPreview alloc] initWithFrame:[UIScreen mainScreen].bounds];
    preview.session = self.manager.session;
    self.view = preview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self startRunning];
}


#pragma mark - Actions
- (void)btnCancelClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - setupUI
-(void) setupUI {
    //
    UIButton * btnCanel = [[UIButton alloc] initWithFrame:CGRectMake(10, 40, 50, 22)];
    [btnCanel setTitle:@"返回" forState:UIControlStateNormal];
    [btnCanel setBackgroundColor:[UIColor orangeColor]];
    
    [self.view addSubview:btnCanel];
    [btnCanel addTarget:self action:@selector(btnCancelClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btnCanel.layer.cornerRadius = 10;
    btnCanel.layer.masksToBounds = YES;
}


#pragma mark - methods
- (void)startRunning {
    // 避免模拟器运行崩溃
    if (!TARGET_IPHONE_SIMULATOR) {
        [self.manager.session startRunning];
    }
}


#pragma mark - delegates
- (void)gxQRCodeManager:(GXQRCodeManager *)gxCodemanager backObject:(NSString *)backObject backObjectType:(BackObjectType)backObjectType {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
        
        // call back
        if ([self.scanDelegate respondsToSelector:@selector(gxScanVc:backObject:backObjectType:)]) {
            [self.scanDelegate gxScanVc:self backObject:backObject backObjectType:backObjectType];
        }
    });
}



@end


/*
 
 #pragma mark - GXScanViewControllerDelegate
 
 //#import "GXScanViewController.h"
 //#import <SafariServices/SafariServices.h>
 //GXScanViewControllerDelegate
 
 
 - (void)btnScanClick:(id)sender {
 GXScanViewController * scanVc = [[GXScanViewController alloc] init];
 scanVc.scanDelegate = self;
 [self presentViewController:scanVc animated:YES completion:nil];
 }
 
 
 - (void)gxScanVc:(GXScanViewController *)scanVc backObject:(NSString *)backObject backObjectType:(BackObjectType)backObjectType {
 //
 if (backObjectType == kObjectTypeString) {
 //
 self.lblMessage.text = backObject;
 NSLog(@"%@", backObject);
 
 }else if (backObjectType == kObjectTypeHyperlink) {
 // safariVc
 NSLog(@"%@", backObject);
 SFSafariViewController * safariVc = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:backObject]];
 [self presentViewController:safariVc animated:YES completion:nil];
 }
 }
 
 */






